﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T13DelegateAndEvent
{
    //定义事件参数：观察者关系的内容
    public class UserEventArgs:EventArgs
    {
        public string Email;
        public string Mobile;
        public string Amount;

        public UserEventArgs(string email, string mobile, string amount)
        {
            Email = email;
            Mobile = mobile;
            Amount = amount;
        }
    }
    //事件发布者：银行
     public  class Bank
    {
        //声明委托：银行交易
        public delegate void ProcessTranEventHangler(object sender, UserEventArgs e);
        //声明事件
        public event ProcessTranEventHangler ProcessTran;

        protected virtual void OnProcessTran(UserEventArgs e)
        {
            if(ProcessTran!=null)
            {
                ProcessTran(this, e);
            }
        }
        //触发事件
        public void Process(UserEventArgs e)
        {
            OnProcessTran(e);
        }
    }
    //事件观察者:电子邮件
    public class Email
    {
        public static void SendMail(object sender,UserEventArgs e)
        {
            Console.WriteLine($"向用户邮件{e.Email}发送邮件：你在{GetNow()}取款金额为{e.Amount}元");
        }

        private static object GetNow()
        {
            return System.DataTime.Now;
        }
    }
    //事件观察者：短信
    public class MobileSMS
    {
        public static void SendNotification(object sender, UserEventArgs e)
        {
            Console.WriteLine($"向用户邮件{e.Mobile}发送短信：你在{System.DataTime.Now}取款金额为{e.Amount}元");
        }
    }
    //订阅系统：松耦合
    public class SubscribeSystem
    {
        public SubscribeSystem() { }
        public SubscribeSystem (Bank bank,UserEventArgs e)
        {
            bank.ProcessTran += Email.SendMail;
            bank.ProcessTran += MobileSMS.SendNotification;
            bank.Process(e);
        }
    }
}
